package test;

public class DWProduct {
	private CSVRecord record;
	//private MappingFromMagento fMapping;
	private MappingBase fMapping;
	
	//public DWProduct (CSVRecord rec) {
	//	record = rec;
	//}

	public DWProduct (CSVRecord rec, MappingBase fMapping) {
		record = rec;
		this.fMapping = fMapping;
	}

	
	public DWObject getProductDWObject() {
		DWObject dwo = new DWObject("product");
		//fMapping = new MappingFromMagento();

		String v;
		
		v = record.getFieldValue( fMapping.mapField("product-id") );
		dwo.setAttribute("product-id", v );

		v = record.getFieldValue( fMapping.mapField("display-name") );
		DWObject disp=dwo.addChildObject("display-name",v);
		disp.setAttribute("lang", "x-default");
		disp.setPrefix("lang", "xml");
		
		v = record.getFieldValue( fMapping.mapField("long-description") );
		v = fMapping.mapValue("long-description", v);
		dwo.addChildObject("long-description", v);
		
		//v=record.getFieldValue(		fMapping.mapField("online-flag"));
		//dwo.addChildObject("online-flag",	fMapping.mapValue("online-flag",v));
		dwo.addChildObject("online-flag",	fMapping.mapValue("online-flag",record));
	
		System.out.println("magento field for \"online-flag\" is:"+fMapping.mapField("online-flag"));
		//System.out.println("map 1 to dw format is:"+formatMapping.mapValue("online-flag","1"));
		System.out.println("map 1 to dw format is:"+fMapping.mapValue("online-flag","1"));
		//dwo.setAttribute("product-id", record.);
		//dwo.addChildObject("display-name",categTree[i]);
		return dwo;
	}

	public DWObject[] getCategoriesAssigned() {
		String[] categoriesAssigned=null;
		DWObject[] dwObjs; 		//DWObject dwo = new DWObject("product");
		
		//String[] categoriesAssigned= record.getFieldValues(fMapping.mapField("category-assignment"));
		//record.getFieldValues(fMapping.mapField("category-assignment"));
		categoriesAssigned=record.getFieldValues(fMapping.mapField("category-assignment"));
		int len=categoriesAssigned.length;
		dwObjs = new DWObject[len];
		//String cId,prefix;
		//String[] idBuf;
		String pId = record.getFieldValue( fMapping.mapField("product-id") );
	
		for (int i=0;i<len;i++) {
			/*
			idBuf = categoriesAssigned[i].split("/");
			cId =""; prefix="";
			for (int j=0, bufLen=idBuf.length;j<bufLen;j++) {
				cId +=prefix+idBuf[j].trim();
				prefix="-";
			}
			*/
			categoriesAssigned[i] = fMapping.mapValue("category-assignment",categoriesAssigned[i]);
			if(categoriesAssigned[i]!=null) {
				dwObjs[i] = new DWObject("category-assignment");
				dwObjs[i].setAttribute("category-id", categoriesAssigned[i]);
				dwObjs[i].setAttribute("product-id", pId);
				dwObjs[i].addChildObject("primary-flag", "true");
			} else {dwObjs[i]=null;}

		}
		return dwObjs;
	}
}
